*********************************************************************
* Attitudinal Ambivalence on Redistribution: Causes and Electoral   *
* 		Implications Across Europe      							*
* Alon Yakter  														*
* Comparative Political Studies (12/2022)							*
*                                                                   *
* ESS variable recoding code					 					* *********************************************************************
* The following code goes through the data recoding steps based on	*
* original ESS variables. This is not a complete replication, which	*
* also involved a lot of manual mergers with CHES and party coding,	*
* but a general guide to recreating the variables in these and 		*
* future ESS datasets. Accordingly, I do not include the original	*
* ESS files in the replication package, but they are freely 		*
* available through the project's website. For ESS 2016 (round 8),	*
* I used version 2.2, production date 10.12.2020. For ESS 2008 		*
* (round 4), I used version 4.5, production date 1.12.2018.			*
*																	*
* The code was executed with Stata 17.                    			*
*********************************************************************

* add numeric code for country (originally a string, allows inclusion as fixed effects, etc.)
encode cntry, gen(cntryn)

* if needed, single weight variable for earlier rounds (alreayd appears in later rounds)
gen anweight = pspwght*pweight

** ambivalence measure
* support for greater government responsibility (dimension 1)
alpha  gvslvol gvslvue gvcldcr, i
factor  gvslvol gvslvue gvcldcr [aw=anweight], pcf
predict govresp
* disapproval of redistribution's consequences (dimension 2)
foreach var of varlist sbstrec sbbsntx sblazy sblwcoa {
	recode `var' (1 = 5)(2 = 4)(4 = 2)(5 = 1), gen(`var'_rvrs)
	}
alpha sbstrec_rvrs sbbsntx_rvrs sblazy_rvrs sblwcoa_rvrs, i
factor sbstrec_rvrs sbbsntx_rvrs sblazy_rvrs sblwcoa_rvrs [aw=anweight], pcf bl(0.4)
predict bnfts_costs
* rescale from 0 to 1
 foreach v of varlist govresp bnfts_costs {
	qui sum `v'  [aw=anweight]
	gen n_`v' = (`v' - r(min)) / (r(max) - r(min))
	}
* adjusted Griffin formula
gen amb_closeness = -abs(n_govresp - n_bnfts_costs)
gen amb_intensity = (abs(.5 - n_govresp) + abs(.5 - n_bnfts_costs))/2
gen ambivalence = amb_intensity + amb_closeness

** demographics
* age group
recode agea (15/24 = 1)(25/34 = 2)(35/44 = 3)(45/54 = 4)(55/64 = 5)(65/100 = 6), gen(agegroup)
* education
recode eisced (55 = .)
* union membership
recode mbtru (3 = 1)(1 = 3), gen(union)
* religiosity
recode rlgatnd (7 = 1)(6 = 2)(5 = 3)(3 = 5)(2 = 6)(1 = 7), gen(religious)
* native born
recode brncntr (1 = 2)(2 = 1), gen(brncntr_op)

* self-attested interest in politics
recode polintr (1 = 4)(2 = 3)(3 = 2)(4 = 1), gen(polintr_pos)

* integrated economic need
foreach v of varlist hincfel hinctnta {
		qui sum `v'  [aw=anweight]
		gen n_`v' = (`v' - r(min)) / (r(max) - r(min))
		}
gen n_hincfel_rev = (1 - n_hincfel)
gen econ_sec = (n_hinctnta + n_hincfel_rev)/2

* Oesch's class scheme (using his code for ESS rounds 2012-2018)
	* run Stata do files for ESS available in Daniel Oesch's website: 
	* https://people.unil.ch/danieloesch/scripts/ (url updated as of November 2022)
	* (remove asterisk in next row after downloading file)
* do "oesch_class_scheme_ESS_2012_2014_2016_2018.do"

** second-dimension attitudes
* anti-immigration
alpha imsmetn imdfetn impcntr imbgeco imueclt imwbcnt, i
factor imsmetn imdfetn impcntr imbgeco imueclt imwbcnt [aw=anweight], bl(0.4) pcf
predict immig_con
* euroskepticism 
recode euftf (0 = 10)(1 = 9)(3 = 8)(4 = 7)(5 = 6)(6 = 5)(7 = 4)(8 = 3)(9 = 2)(10 = 1), gen(euftf_con)
* social conservatism (anti-LGBT)
alpha freehms hmsfmlsh hmsacld, i
factor freehms hmsfmlsh hmsacld, bl(0.4) pcf
predict lgbt_con
	* in 2008 (round 4) most anti-LGBT questions are unavilable - substitue with this code (remove asterisk in next row):
		* tab freehms
		* gen lgbt_con = freehms


** value conflict - individualism vs egalitarianism/solidarity
* revrse order of both dimensions
foreach var of varlist imprich ipeqopt ipshabt ipudrst iphlppl ipsuces iprspot iplylfr {
	recode `var' (1 = 6)(2 = 5)(3 = 4)(4 = 3)(5 = 2)(6 = 1), gen(`var'_op)
}
* exploratory factor analysis demonstrating two dimensions
factor imprich_op ipeqopt_op ipshabt_op ipudrst_op iphlppl_op ipsuces_op iprspot_op iplylfr_op, bl(0.4)
rotate, bl(0.4)
* individualism
factor imprich_op ipshabt_op ipsuces_op iprspot_op, bl(0.4) pcf
predict indv 
* egalitarianism/solidarity
factor ipeqopt_op ipudrst_op iphlppl_op iplylfr_op, bl(0.4) pcf
predict cmnt
* rescale from 0 to 1
foreach v of varlist indv cmnt {
	qui sum `v'  [aw=anweight]
	gen n_`v' = (`v' - r(min)) / (r(max) - r(min))
	}
* calculate value conflict (negative distance)
gen val_conf = 1 - abs(n_indv - n_cmnt)

** oncongruence between general support for redistribution and left-right self-id
* general support for redistribution - reverse order (such that greater = more support)
recode gincdif (5 = 1)(4 = 2)(2 = 4)(1 = 5), gen(gnrl_rdst)
* rescale from 0 to 1
foreach v of varlist gnrl_rdst lrscale {
	qui sum `v'  [aw=anweight]
	gen n_`v' = (`v' - r(min)) / (r(max) - r(min))
	}
* calculate incongruence (negative distance)
gen incg_rdst_lr = 1 - abs(n_gnrl_rdst - n_lrscale)

* for better interpertation, standardize all remaining DVs on 0-1 scale
foreach v of varlist immig_con euftf_con lgbt_con polintr_pos eisced union /// 
	gndr agegroup religious brncntr_op domicil {
		qui sum `v'  [aw=anweight]
		gen n_`v' = (`v' - r(min)) / (r(max) - r(min))
		}

